<?php
class SMTPClient
{
	function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $cc, $bcc, $subject, $body)
	{
		$this->SmtpServer = $SmtpServer;
		$this->SmtpUser = base64_encode ($SmtpUser);
		$this->SmtpPass = base64_encode ($SmtpPass);
		$this->from = $from;
		$this->to = $to;
		$this->toArray = SMTPClient::decomposeEmails($to);
		$this->cc = $cc;
		$this->ccArray = SMTPClient::decomposeEmails($cc);
		$this->bcc = $bcc;
		$this->bccArray = SMTPClient::decomposeEmails($bcc);
		$this->subject = $subject;
		$this->body = $body;

		if ($SmtpPort == "")
		{
			$this->PortSMTP = 25;
		}
		else
		{
			$this->PortSMTP = $SmtpPort;
		}
	}
	
	public static function decomposeEmails ( $str/*, $field = 'RCPT TO'*/ )
	{
		$res = array();
		$str = preg_replace ( '/\s/', '', $str );
		$res = preg_split ( '/,/', $str );
		/*$a = preg_split ( '/,/', $str );
		foreach ( $a as $v )
		{
			$res[] = $field . ': <' . $v . '>\r\n';
		}*/
		return $res;
	}

	function SendMail ()
	{
		$talk['result'] = 'OK';
		$talk["logs"] = '';
		if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
		{
			$v = "EHLO ".$_SERVER['HTTP_HOST']."\r\n";
			$talk["logs"] .= '>> ' . $v;
			fputs ($SMTPIN, $v);
			
			$v = fread ( $SMTPIN, 1024*10 );
			$talk["logs"] .= '<< ' . $v;
			$talk["hello"] = $v;
			
			$v = "auth login\r\n";
			$talk["logs"] .= '>> ' . $v;
			fputs($SMTPIN, $v);
			
			$v = fgets($SMTPIN,1024);
			$talk["res"]=$v;
			$talk["logs"] .= '<< ' . $v;
			
			$v = $this->SmtpUser."\r\n";
			fputs($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			
			$v = fgets($SMTPIN,1024);
			$talk["logs"] .= '<< ' . $v;
			$talk["user"]=$v;
			
			$v = $this->SmtpPass."\r\n";
			fputs($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			
			$v = fgets($SMTPIN,256);
			$talk["pass"]=$v;
			$talk["logs"] .= '<< ' . $v;
			
			$v = "MAIL FROM: <".$this->from.">\r\n";
			fputs ($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			
			$v = fgets ( $SMTPIN, 1024 );
			$talk["logs"] .= '<< ' . $v;
			$talk["From"] = $v;
			$ok = intval($talk["From"]) == 250;
			if ( !$ok )
			{
				$v = "QUIT\r\n";
				fputs ($SMTPIN, $v);
				$talk["logs"] .= '>> ' . $v;
				fclose($SMTPIN);
				$talk['result'] = 'Error: ' . $talk["From"];
				return $talk;
			}
			
			$tos = array_merge ( $this->toArray, $this->ccArray, $this->bccArray );
			$talk["To"] = '';
			foreach ( $tos as $v )
			{
				if ( !empty($v) )
				{
					$v2 = 'RCPT TO: <' . $v . ">\r\n";
					$talk["logs"] .= '>> ' . $v;
					fputs ($SMTPIN, $v2);
					
					$v2 = fgets ($SMTPIN, 1024);
					$talk["logs"] .= '<< ' . $v2;
					$talk["To"] .= $v2;
					$ok = intval($v2) == 250;
					if ( !$ok )
					{
						$v2 = "QUIT\r\n";
						fputs ($SMTPIN, $v2);
						$talk["logs"] .= '>> ' . $v2;
						fclose($SMTPIN);
						$talk['result'] = 'Error: ' . $talk["To"];
						return $talk;
					}
				}
			}
			$v = "DATA\r\n";
			fputs($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			
			$v = fgets( $SMTPIN,1024 );
			$talk["logs"] .= '<< ' . $v;
			$talk["data"]=$v;
			$ok = intval($talk["data"]) == 354;
			if ( !$ok )
			{
				$v = "QUIT\r\n";
				fputs ($SMTPIN, $v);
				$talk["logs"] .= '>> ' . $v;
				fclose($SMTPIN);
				$talk['result'] = 'Error: ' . $talk["data"];
				return $talk;
			}
			// TODO: corriger cas où plusieurs receveurs (to, cc, bcc)
			$to2 = '';
			foreach ( $this->toArray as $v )
			{
				$to2 .= ', <' . $v . ">"; // en théorie on peut aussi fr : Nom <email>, ...
			}
			$to2 = substr ( $to2, 2 );
			$cc2 = '';
			if ( count($this->ccArray) != 0 )
			{
				foreach ( $this->ccArray as $v )
				{
					$cc2 .= ', <' . $v . ">"; // en théorie on peut aussi fr : Nom <email>, ...
				}
				$cc2 = substr ( $cc2, 2 );
				$cc2 = 'Cc: ' . $cc2;
			}
			$bzbz = "From: <".$this->from.">\r\nTo: <".$this->to.">\r\n" . $cc2 . "\r\nSubject: ".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n";
			//echo '<pre>'.$bzbz.'</pre>'; // debug
			$v = $bzbz;
			fputs($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			
			$v = fgets($SMTPIN,256);
			$talk["logs"] .= '<< ' . $v;
			$talk["send"]=$v;
			$ok = intval($talk["send"]) == 250;
			if ( !$ok )
				$talk['result'] = 'Error: ' . $talk["send"];
			//CLOSE CONNECTION AND EXIT ...
			$v = "QUIT\r\n";
			fputs ($SMTPIN, $v);
			$talk["logs"] .= '>> ' . $v;
			fclose($SMTPIN);
		}
		return $talk;
	}
}
?>
